﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using aaaSoft.SkinEngine.SkinHelpers;

namespace aaaSoft.SkinEngine.MyControls
{
    class MyProgressBar : NativeWindow, IMyControl
    {
        ProgressBar pbBase;
        SkinEngine skinEng;

        public MyProgressBar(ProgressBar pb, SkinEngine skinEng)
        {
            pbBase = pb;
            this.skinEng = skinEng;
        }

        public void StopControlSkin()
        {
            ReleaseHandle();
        }

        public void StartControlSkin()
        {
            AssignHandle(pbBase.Handle);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == SkinHelpers.UnsafeNativeMethods.NativeMethods.WM_PAINT)
            {
                DrawProgressBar();
            }
        }

        private void DrawProgressBar()
        {
            Rectangle rectangle4;

            Graphics graphics;

            //填充背景(Windows系统有效)
            try
            {
                IntPtr hDC = SkinHelpers.UnsafeNativeMethods.GetWindowDC(base.Handle);
                graphics = Graphics.FromHdc(hDC);
                graphics.FillRectangle(new SolidBrush(skinEng.BackColor), new Rectangle(0, 0, pbBase.Width + 10, pbBase.Height + 10));
                SkinHelpers.UnsafeNativeMethods.ReleaseDC(base.Handle, hDC);
            }
            catch { }
            //画进度条
            graphics = pbBase.CreateGraphics();

            SolidBrush brush = new SolidBrush(pbBase.ForeColor);
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            Rectangle rect = new Rectangle(1, 1, pbBase.Width - 2, pbBase.Height - 2);
            graphics.FillRectangle(new SolidBrush(pbBase.BackColor), rect);
            rect.Width = Convert.ToInt32((double)(((double)(pbBase.Value * (pbBase.Width - 2))) / ((double)pbBase.Maximum)));


            Rectangle rectangle2 = new Rectangle(1, 1, rect.Width, Convert.ToInt32((double)(((double)rect.Height) / 4.0)) + 1);
            Rectangle rectangle3 = new Rectangle(1, Convert.ToInt32((double)(((double)rect.Height) / 4.0)), rect.Width, Convert.ToInt32((double)(((double)(rect.Height * 3)) / 4.0)) + 1);
            if ((rectangle3.Top + rectangle3.Height) != (pbBase.Height - 2))
            {
                rectangle3.Height += ((pbBase.Height - 1) - rectangle3.Top) - rectangle3.Height;
            }
            Point point = new Point(rectangle2.Left, rectangle2.Top);
            Point point2 = new Point(rectangle2.Left, rectangle2.Top + rectangle2.Height);

            LinearGradientBrush brush2 = new LinearGradientBrush(point, point2, SkinHelpers.ColorHelper.GetDeeperColor(skinEng.BackColor, 2f), SkinHelpers.ColorHelper.GetDeeperColor(skinEng.BackColor, 0.9f));
            graphics.FillRectangle(brush2, rectangle2);

            //画进度部分
            point2 = new Point(rectangle3.Left, rectangle3.Top - 1);
            point = new Point(rectangle3.Left, rectangle3.Top + rectangle3.Height);
            brush2 = new LinearGradientBrush(point2, point, ColorHelper.GetDeeperColor(ColorHelper.GetDLColor(skinEng.BackColor), 0.8f), ColorHelper.GetDLColor(skinEng.BackColor));
            graphics.FillRectangle(brush2, rectangle3);
            rectangle2.X = rectangle2.Width + 1;
            rectangle2.Width = (pbBase.Width - 2) - rectangle2.Width;
            rectangle3.X = rectangle2.X;
            rectangle3.Width = rectangle2.Width;
            point2 = new Point(rectangle2.Left, rectangle2.Top);
            point = new Point(rectangle2.Left, rectangle2.Top + rectangle2.Height);
            brush2 = new LinearGradientBrush(point2, point, Color.White, Color.WhiteSmoke);
            graphics.FillRectangle(brush2, rectangle2);
            point2 = new Point(rectangle3.Left, rectangle3.Top - 1);
            point = new Point(rectangle3.Left, rectangle3.Top + rectangle3.Height);
            brush2 = new LinearGradientBrush(point2, point, SkinHelpers.ColorHelper.GetDeeperColor(Color.LightGray, 1f), SkinHelpers.ColorHelper.GetDeeperColor(Color.LightGray, 1.2f));
            graphics.FillRectangle(brush2, rectangle3);
            brush2.Dispose();

            //画圆角部分
            rectangle4 = new Rectangle(0, 0, pbBase.Width - 2, pbBase.Height - 2);
            graphics.DrawRectangle(Pens.DarkGray, rectangle4);
            Pen pen = new Pen(Color.DarkGray, 1f);
            graphics.DrawArc(pen, -1, 0, 8, 8, -180, 90);
            graphics.DrawArc(pen, (pbBase.Width - 8) - 1, 0, 8, 8, -90, 90);
            graphics.DrawArc(pen, (pbBase.Width - 8) - 2, (pbBase.Height - 8) - 1, 8, 8, 0, 90);
            graphics.DrawArc(pen, 0, (pbBase.Height - 8) - 1, 8, 8, 90, 90);

            brush.Dispose();
            pen.Dispose();
        }
    }
}
